﻿package  com{
	
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.URLRequestMethod;
	import fl.containers.UILoader;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import caurina.transitions.*;
	import flash.events.*;
	import flash.text.*;
	import flash.filters.*;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import com.Thumbnail;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import fl.controls.ProgressBar; 
	import flash.events.ProgressEvent; 
	import flash.display.StageDisplayState;
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import com.ThumbnailContener;
	
	
	
	public class Main extends MovieClip {
		static var nb_images:Number = 0;
		

		//Conteneur des vignettes
		static var thumb_container;
		

		/***Config XML vars***/
		static var image_directory:String;
		static var thumb_directory:String;
		static var distant_server:String;
		static var animation_width:Number;
		static var animation_height:Number;
		static var thumb_width:Number;
		static var thumb_height:Number;
		static var drop_shade_size:Number;
		static var column_gap:Number;
		static var row_gap:Number;
		static var columns:Number;
		/********************/
		
		static var myXML;
		
		static public var current_thumb;
		
		
		static var key = '';
		
		static var animator_name;
		
		static var debug_txt;
		
		public function Main() {
			
			debug_txt = debug;
			
			animator_name = click_name;
			
			form.envoyer.addEventListener(MouseEvent.CLICK, send_message);
			
			//Chargeur qui téléchargera notre fichier XML	
			var chargeur2:URLLoader = new URLLoader ();
			
			//Chargeur qui téléchargera notre fichier XML	
			var chargeur:URLLoader = new URLLoader ();
			//objet URLRequest qui contient l'url du fichier XML:
			var adresse:URLRequest = new URLRequest (ADTECH.getFileUrl("galerie_msg.xml"));
							
			//Config url
			var adresse2:URLRequest = new URLRequest (ADTECH.getFileUrl('config_photo_msg.xml'));
			
			chargeur2.load(adresse2);
			chargeur2.addEventListener(Event.COMPLETE, record_config);
			
			chargeur.load(adresse);
			chargeur.addEventListener(Event.COMPLETE, finDuChargementXML);
			
			
			//Variable contenant le texte alternatif
			var alt:String;
		}
		
		
		function record_config(evt:Event):void {			
			var config_xml = new XML(evt.target.data);
			animation_width = config_xml.animation_width;
			animation_height = config_xml.animation_height;
			thumb_width = config_xml.thumb_width;
			thumb_height = config_xml.thumb_height;
			distant_server = config_xml.distant_server;
			image_directory = config_xml.image_directory;
			thumb_directory = config_xml.thumb_directory;
			column_gap = config_xml.column_gap;
			row_gap = config_xml.row_gap;
			drop_shade_size = config_xml.drop_shade_size;
			columns = config_xml.columns;
		}
		
		
		/*Une fois que le fichier XML est chargé...*/
		function finDuChargementXML(evt:Event):void {
			myXML = new XML(evt.target.data);
			var thumbnail_contener = new ThumbnailContener(myXML);
			
			thumbnail_contener.createThumbs()
			
			addChild(thumbnail_contener);
			
			//contener.addChild( thumb_container );

		}
		
		
		
		
		//transition in function
		static function onClick(my_thumb):void {
			ThumbnailContener.current_clicked = my_thumb.num;
			
			//resetThumbs(my_thumb.num);
			my_thumb.clicked = true;

			//Si l'image sur laquelle on a cliquée est déjà selectionnée, il ne se passe rien....
			if(ThumbnailContener.src == myXML.image[my_thumb.num].attribute("src")){
				//trace('déjà cliquée');
				return;
			}
			
			ThumbnailContener.src = myXML.image[my_thumb.id].attribute("src");
			var alt = myXML.image[my_thumb.id].attribute("alt");
			animator_name.text = alt;
			animator_name.alpha = 0;
			Tweener.addTween(animator_name, {alpha:1, time:1, transition:"easeOut"});
			
						
			define_key(alt)

			
			ADTECH.event(alt);
			

		}
		
		
		static function define_key(name) {
			if( name == 'Difool' ) {
				key = 'ZGlmb29sLWF1ZGl0ZXVyQHNreXJvY2suY29t';
			}
			
			else if( name == 'Marie' ) {
				key = 'bWFyaWVAc2t5cm9jay5jb20='
			}
			
			else if( name == 'Romano' ) {
				key = 'cm9tYW5vQHNreXJvY2suY29t'
			}
			
			else if( name == 'Cédric' ) {
				key = 'Y2VkcmljQHNreXJvY2suY29t'
			}
			
			
			else if( name == 'Sami' ) {
				key = 'c2FteUBza3lyb2NrLmNvbQ=='
			}
			
			else if( name == 'Karim' ) {
				key = 'a2FyaW1Ac2t5cm9jay5jb20='
			}
			
			else if( name == 'Momo' ) {
				key = 'bW9tb0Bza3lyb2NrLmNvbQ==';
				
			}
			
		}
		

		function send_message(event:MouseEvent) {
			var prenom = form.prenom.text;
			var message = form.message.text;
			var email = form.email.text;
			
			
			if( key != '' ) {
			
				if( prenom != '' && message != '' && email != '' ) {
					
					if (isValidEmail(email)) {
						var urlLoader:URLLoader = new URLLoader();
	
						//trace(signature);
						var urlRequest:URLRequest = new URLRequest("http://www.skyrock.fm/front/sendmail.php?mail="+key);
						//var urlRequest:URLRequest = new URLRequest("http://projets-regie3.aaazzouz.svn2.orbus.fr/canvas/operations/Radio%20Libre/message/test.php");
						urlRequest.data = new URLVariables("msg=" + message + "&prenom=" + prenom + "&expediteur=" +email+"&send=1");
						//urlRequest.data = new URLVariables("msg=" + message + "&prenom=" + prenom + "&expediteur=" +email+ "&mail=" +key);
						urlRequest.method = URLRequestMethod.POST;
		
						urlLoader.addEventListener(Event.COMPLETE, donneesChargees);
		
						urlLoader.load(urlRequest);
						
					} else {
						txt_error.text = 'Email invalide.';
					
					}

				} else {
					txt_error.text = 'Remplis tous les champs.';
					trace('error');
				}
				
			
			} else {
				txt_error.text = 'Choisis un animateur.';
				
			}
			
			txt_error.alpha = 0;
			Tweener.addTween(txt_error, {alpha:1, time:1, transition:"easeOut"});
			
			
		}
		
		
		private function isValidEmail(email:String):Boolean {
			var emailExpression:RegExp = /([a-z0-9._-]+?)@([a-z0-9.-]+)\.([a-z]{2,4})/;
			return emailExpression.test(email);
		}
		
		/*
		*
		traiter les données en retour du script PHP
		*
		*/
		function donneesChargees(e:Event):void
		{
			
			if (e.target.data != '')
			{
				
				
				

			}
			

		}
		
		
	}
	
}


